<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/14
 * Time: 15:24
 */

namespace app\admin\model;


use think\Model;

class UUser extends Model
{
    /**
     * 查询所有用户
     * @param $field
     * @param $where
     * @param $page
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getUserLists($field, $where, $page, $limit){
        $lists = $this->alias('uuu')
            ->join('up_mechanism um', 'um.mechanism_id = uuu.user_to_mechanism_id', 'left')
            ->field($field)
            ->where($where)
            ->order('uuu.user_id desc')
            ->page($page, $limit)
            ->select();
        return $lists;
    }
    
    /**
     * 获取条件搜索的记录总数，用于分页
     * @param $where  搜索条件
     * @return int|string
     */
    public function countNum($where){
        $count = $this->alias('uuu')
            ->join('up_mechanism um', 'um.mechanism_id = uuu.user_to_mechanism_id', 'left')
            ->where($where)
            ->count();
        return $count;
    }
    
    /**
     * 添加用户
     * @param $data
     * @return false|int
     */
    public function insertUser($data){
        $res = $this->allowField(true)->save($data);
        return $res;
    }
    
    /**
     * 获取本机构的志愿者的最大编号
     * @param $mechanism_id
     * @return mixed
     */
    public function getMaxNum($mechanism_id){
        $maxNum = $this->where('user_to_mechanism_id', $mechanism_id)->max('user_number');
        return $maxNum;
    }
    
    /**
     * 获取认证信息
     * @param $user_id
     * @param $field
     * @return array|false|\PDOStatement|string|Model
     */
    public function getAttestation($user_id, $field){
        $info = $this->alias('uuu')
            ->join('up_mechanism um', 'um.mechanism_id = uuu.user_to_mechanism_id', 'left')
            ->join('up_area ua', 'ua.district_id = uuu.user_shi', 'left')
            ->join('up_assortment ua1', 'ua1.assortment_id = uuu.user_hospital', 'left')//医院
            ->join('up_assortment ua2', 'ua2.assortment_id = uuu.user_department', 'left')//科室
            ->join('up_assortment ua3', 'ua3.assortment_id = uuu.user_title', 'left')//职称
            ->join('up_assortment ua4', 'ua4.assortment_id = uuu.user_post', 'left')//职务
            ->where('uuu.user_id', $user_id)
            ->field($field)
            ->find();
        return $info;
    }
    
    /**
     * 更改普通用户状态
     * @param $status 普通用户状态
     * @param $id   ID
     * @return array
     */
    public function uUserStatus($status, $id){
        if($status == 2){
            $msg = '禁用';
        }else{
            $msg = '启用';
        }
        $uUserStatus = $this->where('user_id', $id)->setField('user_status', $status);
        if(!$uUserStatus){
            return ['code' => 100, 'data' => '', 'msg' => $msg . '失败'];
        }else{
            return ['code' => 200, 'data' => '', 'msg' => $msg . '成功'];
        }
    }
    
    /**
     * 获取某个志愿者的编号
     * @param $id
     * @return mixed
     */
    public function getUserCode($id){
        $res = $this->where('user_id', $id)->value('user_number');
        return $res;
    }
    
    /**
     * 更新志愿者编号
     * @param $id
     * @param $data
     * @return $this
     */
    public function updateCode($id, $data){
        $res = $this->where('user_id', $id)->update($data);
        return $res;
    }
}